package com.ethan.cj.ssm.service.impl;


import com.ethan.cj.ssm.dao.ContentDao;
import com.ethan.cj.ssm.entity.Content;
import com.ethan.cj.ssm.service.ContentService;
import com.ethan.cj.ssm.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

import java.util.Map;

@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private ContentDao contentDao;


    /**
     * 根据分类查询内容信息
     * @param caid 分类ID
     * @return 内容集合
     */
    @Override
    public Map<Object, Object> selectContentByCaid(int caid) {

        List<Content> list = contentDao.selectContentByCaid(caid);
        for (Content c : list) {
            if (c.getState().equals("0"))
                c.setState("未发布");
            if (c.getState().equals("1"))
                c.setState("已发布");
        }
        Map<Object, Object> result = new HashMap<>();
        if (list != null && list.size() > 0) {
            result.put("code", 0);
            result.put("msg", "");
            result.put("data", list);
        } else {
            result.put("code", 1);
            result.put("msg", "无数据");
        }
        return result;
    }



    /**
     * 分页查询
     *
     * @param page
     * @param limit
     * @param map
     * @return
     */
    @Override
    public Map<Object, Object> getContentByTheme(int page, int limit, Map<String, Object> map) {
        Map<Object, Object> result = new HashMap<>();
        int currentIndex = (page - 1) * limit;
        System.out.println(map.get("theme"));
        map.put("pageSize", limit);
        map.put("currentIndex", currentIndex);
        int count = contentDao.getTotal(map);
        List<Content> list = contentDao.getContentByTheme(map);
        if (list != null && list.size() > 0) {
            result.put("code", 0);
            result.put("data", list);
            result.put("msg", "");
            result.put("count", count);
        } else {
            result.put("msg", "无数据");
            result.put("code", 1);
        }
        return result;
    }

    /**
     * 根据ID查询某条信息进行回显
     *
     * @param cid
     * @return
     */
    @Override
    public Content selectContentCid(int cid) {
        return contentDao.selectContentCid(cid);
    }

    /**
     * 修改信息
     *
     * @param contentJson
     * @return
     */
    @Override
    public Map<Object, Object> updateContent(String contentJson) {
        Content content = JsonUtil.parseObject(contentJson, Content.class);
        int rst = contentDao.updateContent(content);
        Map<Object, Object> result = new HashMap<>();
        if (rst > 0) {
            result.put("msg", "成功修改信息！");
            result.put("code", 1);
        } else {
            result.put("code", 0);
            result.put("msg", "修改信息失败！");
        }
        return result;
    }

    /**
     * 删除一条信息
     * @param cid
     * @return
     */
    @Override
    public Map<Object, Object> deleteContentCid(int cid) {
        int rst = contentDao.deleteContentCid(cid);
        Map<Object, Object> result = new HashMap<>();
        if (rst > 0) {
            result.put("msg", "成功删除信息！");
            result.put("code", 1);
        } else {
            result.put("code", 0);
            result.put("msg", "删除信息失败！");
        }
        return result;
    }
}
